<?php
//error, action, supplierList, supplierName, supplierCode, idNumOnly, description, price
function renderForm($error, $action, $supplierList, $supplierName, $supplierCode, $IDNumOnly, $description, $price)
{
?>
<!----------HTML CODES----------------------------------------------------------------------------------->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta name="Description" content="Clinic Management System" />
	<link rel="stylesheet" href="../includes/Style.css" type="text/css" />
	<title>OPTICSoft</title>
</head>
<body>
<?php include('../includes/header.php');?>
<div id="content-wrap"><div id="content">

<?php displayMessage(); ?>
<?php include('sidebar.php');?>

<div id="main">	<div class="post">	
	<h1>Item Info</h1>				
	
	 <form name="main" action="" class="searchform" method="POST">
  	 <table>
  	  <tr>
  		<td align="right">Supplier</td>
  		<td align="left">
  		<?php
  		  if ($supplierName != '')
  		    echo '<select id="supplier" onChange="SelectSupplier()" disabled="true">';
  	          else
  		    echo '<select id="supplier" onChange="SelectSupplier()">';
  		    
  		  if($supplierList!=null)
		  {
		    while ($row = mysql_fetch_assoc($supplierList)) {
		    	if ($supplierName == $row["Supplier"])
		          echo '<option value="'.$row["Code"].'" selected="true">'.$row["Supplier"];
		        else
		          echo '<option value="'.$row["Code"].'">'.$row["Supplier"];
		   }   
		    
		  }
		?>
  		</select>
  		</td>
  	  </tr>
  	  <tr>
  		<td align="right">Item Code</td>
  		<td align="left"><input type="text" id="code" name="code" size='5' readonly='true' value="<?php echo $supplierCode; ?> " />
  		<input type="text" id="ID" name="id" size='11' onKeyUp='CheckCodeAvailability()' value="<?php echo $IDNumOnly; ?> " /></td>
  		
  	  </tr>  	  
  	   <tr>
  		<td align="right">Description</td>
  		<td align="left"><input type="text" name="description" value="<?php echo $description; ?> " /></td>
  	  </tr>
  	   <tr>
  		<td align="right">Price</td>
  		<td align="left"><input type="text" name="price" value="<?php echo $price; ?> " /></td>
  	  </tr>
  	  <tr> 
  	  	<td align="right"> 		
	 	  <input name="submit" class="button" value="<?php echo $action; ?> " type="submit" />
	 	</td>
  	  </tr>
  	 </table> 
  	 </form>
  	 
</div></div></div></div>
</body>
</html>
<!----------------------------------------------------------------------------------------------------------->
<?php
}
  //include statement
  require("item.inc");
  $item = new Item();
  global $error;
  $supplierList = $item->retrieveSuppliers();
  
  //Check if the form hasn't been submitted
  if (!isset($_GET['ID']) && !isset($_POST['submit'])  )  //NO RECORD SELECTED, NEW CLIENT
  {  	  
  	 //error, action, supplierList, supplierName, supplierCode, idNumOnly, description, price
         renderForm('','Add', $supplierList, '', '', '', '', '');  
  }
  else if (isset($_GET['ID']) && !isset($_POST['submit'])  )   //RECORD SELECTED FOR UPDATE
  {
         $item->retrieveByID($_GET['ID']);
  	 renderForm('','Update', $supplierList, $item->supplierName, $item->supplier, $item->IDNumOnly, $item->desc, $item->price);
  }
  //Check if the form has been submitted for ADD
  else if (isset($_POST['submit']) && $_POST['submit'] == 'Add ')
  {
     if (checkInput() == false)
         renderForm("ERROR: Please fill in all required fields!",'Add', '', '', '', '', '', '' );
     else
     {
	//SAVE NEW RECORD
        if($item->add() == '1')
	 $message = "1 record added.";
	else
	 $message = "ERROR Item not added.";
	setcookie("message", $message, time() + 3);
	header('location:item.php');
	exit();
     }
  }
  //Check if the form has been submitted for UPDATE
  else if (isset($_POST['submit']) && $_POST['submit'] == 'Update ')
  {
      if (checkInput() == false)
      {
      	 $item->retrieveByID($_GET['ID']); 
      	 renderForm("ERROR: Please fill in all required fields!",'Update', $supplierList, 
            $item->supplierName, $item->supplier, $item->IDNumOnly, $item->desc, $item->price);
      }
      else
      {
        //UPDATE CURRENT RECORD        
        if($item->update($_GET['ID']) == '1')
	 $message = "Record updated.";
	else
	 $message = "ERROR Unable to complete operation.";;
	setcookie("message", $message, time() + 3);
	header('location:itemList.php');
	exit();
      }
  }
  
  //FUNCTIONS
  function CheckInput()
  {
     if ($_POST['id'] == '')
     	return false;
     return true;
  }
  function displayMessage()
  {
     if (isset($error) && $error != '')
      echo '<div id="notification">'.$error.'</div>';
     if(isset($_COOKIE['message']))
       echo '<div id="notification">'.$_COOKIE["message"].'</div>';
  }
?>

<!--------------------------SCRIPT------------------------------------------>
<script type='text/javascript'>
function SelectSupplier()
{
    var e = document.getElementById("supplier");
    document.getElementById("code").value = e.options[e.selectedIndex].value;
	
}

function CheckCodeAvailability()
{
   //var count = 
   //	   <?php 
   //	     $code = $_POST["code"].$_POST["ID"];
   //	     $result=$item->retrieveByID($code);
   //	     echo mysql_num_rows($result);
   //	   ?>
   //if (count>0)
   //{
   //  var element = document.getElementById("id");
   //  element.style.backgroundColor='#fba7a7';
   //}   
}
</script>
